package com.bailugansan.filter;

import com.bailugansan.pojo.User;
import com.bailugansan.util.Constants;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author BailuGansan
 */
public class SysFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;
        // 获取登录对象中 Session 中存放的 User 对象
        User user = (User)req.getSession().getAttribute(Constants.USER_SESSION);

        if(user == null){
            resp.sendRedirect(req.getContextPath()+"/error.jsp");
        }
        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {
    }
}
